package com.cup.bddm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

public class DateUtil {

	private final static Logger logger = Logger.getLogger(DateUtil.class);

	public static boolean isValidDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

		try {
			Date dateformat = sdf.parse(date);

			return date.equals(sdf.format(dateformat));
		} catch (Exception e) {
			return false;
		}
	}

	public static String getPreday(String curday) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date curDt = new Date();
		String preDt;
		try {
			curDt = sdf.parse(curday);
			Date d = addDate(curDt, -1);
			preDt = sdf.format(d);
			return preDt;
		} catch (ParseException e) {
			logger.error("parse date error", e);
			return null;
		}
	}

	public static String getPreNday(String curday, int n) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date curDt = new Date();
		String preDt;
		try {
			curDt = sdf.parse(curday);
			Date d = addDate(curDt, -n);
			preDt = sdf.format(d);
			return preDt;
		} catch (ParseException e) {
			logger.error("parse date error", e);
			return null;
		}
	}

	public static String getNextDay(String curday) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date curDt = new Date();
		String preDt;
		try {
			curDt = sdf.parse(curday);
			Date d = addDate(curDt, 1);
			preDt = sdf.format(d);
			return preDt;
		} catch (ParseException e) {
			logger.error("parse date error", e);
			return null;
		}
	}

	public static Date addDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}

	public static long getMillis(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	public static String getPreNMonth(String month, int n) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		Date date = null;
		try {
			date = sdf.parse(month);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.MONTH, -n);
		date = cl.getTime();
		return sdf.format(date);
	}

	public static List<String> getPreNMonthList(String month, int n) {
		List<String> monthList = new ArrayList<String>();
		for (int i = n; i >= 1; i--) {
			monthList.add(getPreNMonth(month, i));
		}
		return monthList;
	}

	public static List<String> getMonthPreDateList(String date) {

		List<String> dateList = new ArrayList<String>();

		if (date.length() != 8) {
			logger.error("date len is not 8 error");
			return null;
		}
		int n = Integer.valueOf(date.substring(6, 8));
		for (int i = n - 1; i >= 0; i--) {
			dateList.add(getPreNday(date, i));
		}
		return dateList;
	}

	public static void main(String[] args) {
		String date = "20180112";
		String month = date.substring(0, 6);
		System.out.println(month);
	}
}
